import json

from pyecharts import options
from pyecharts.charts import Line
from pyecharts.options import LabelOpts

with open("../美国.txt", "r", encoding="UTF-8") as f:
    old_content = f.read()
    content = old_content.removeprefix("jsonp_1629344292311_69436(")
    content = content.removesuffix(");")
    usa_content = json.loads(content)

with open("../印度.txt", "r", encoding="UTF-8") as f:
    old_content = f.read()
    content = old_content.removeprefix("jsonp_1629350745930_63180(")
    content = content.removesuffix(");")
    yd_content = json.loads(content)

with open("../日本.txt", "r", encoding="UTF-8") as f:
    old_content = f.read()
    content = old_content.removeprefix("jsonp_1629350871167_29498(")
    content = content.removesuffix(");")
    rb_content = json.loads(content)


def get_data(content, start, end):
    data = content["data"][0]["trend"]
    return data["updateDate"][start:end], data["list"][0]['data'][start:end]


# 获取x, y数据
usa_x_data, usa_y_data = get_data(usa_content, 0, 314)
rb_x_data, rb_y_data = get_data(rb_content, 0, 314)
yd_x_data, yd_y_data = get_data(yd_content, 0, 314)

# 生成图表
line = Line()

line.add_xaxis(usa_x_data)  # 只添加一个，因为共用日期

line.add_yaxis("美国确诊人数", usa_y_data, label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数", rb_y_data, label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数", yd_y_data, label_opts=LabelOpts(is_show=False))

# 生成全局选项
line.set_global_opts(
    title_opts=options.TitleOpts(title="2020年新冠（美国、日本、印度）每日确诊人数", pos_left="center", pos_bottom="1%"),
)
line.render()
